package org.dfyy.postgisdemo.controller;

import lombok.RequiredArgsConstructor;
import org.dfyy.postgisdemo.mapper.RadarMapper;
import org.dfyy.postgisdemo.pojo.DmTrade;
import org.dfyy.postgisdemo.pojo.Radar;
import org.dfyy.postgisdemo.pojo.dto.GeometryDto;
import org.dfyy.postgisdemo.pojo.dto.GeometryVo;
import org.dfyy.postgisdemo.service.DmTradeService;
import org.dfyy.postgisdemo.service.RadarService;
import org.dfyy.postgisdemo.uitls.ResponseUtil;
import org.dfyy.postgisdemo.uitls.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 书山
 * @version 1.0
 * @date 2024-04-12 13:59
 */
@RestController
@RequestMapping("/radar")
@RequiredArgsConstructor
@CrossOrigin
public class RadarController {

    private final RadarService service;
    private final DmTradeService dmTradeService;

    /*
        INSERT
     */
    // 新增点
    @PostMapping("/savePoint")
    public ResultUtil<Boolean> savePoint(@RequestBody Radar radar){
        return ResponseUtil.ok(service.savePoint(radar));
    }

    // 新增线
    @PostMapping("/saveLine")
    public ResultUtil<Boolean> saveLine(@RequestBody GeometryDto geometryDto){
        System.err.println(geometryDto);
         return ResponseUtil.ok(service.saveLine(geometryDto));
    }

    // 新增面
    @PostMapping("/savePolygon")
    public ResultUtil<Boolean> savePolygon(@RequestBody GeometryDto geometryDto){
        System.err.println(geometryDto);
        return ResponseUtil.ok(service.savePolygon(geometryDto));
    }

    /*
        DELETE
     */
    // 批量删除要删除的数据
    @DeleteMapping("/batchDeleteByIds")
    public ResultUtil<Boolean> batchDeleteByIds(@RequestBody List<Integer> ids){
        return ResponseUtil.ok(service.batchDeleteByIds(ids));
    }



    /*
        UPDATE
     */

    // 根据Radar对象 修改点
    @PutMapping("/updatePoint")
    public ResultUtil<Boolean> updatePoint(@RequestBody Radar radar){
        return ResponseUtil.ok(service.updatePoint(radar));
    }


    // 根据Geometry对象 修改线
    @PutMapping("/updateLineString")
    public ResultUtil<Boolean> updateLineString(@RequestBody GeometryDto dto){
        return ResponseUtil.ok(service.updateLineString(dto));
    }


    // 根据Geometry对象 修改面
    @PutMapping("/updatePolygon")
    public ResultUtil<Boolean> updatePolygon(@RequestBody GeometryDto dto){
        return ResponseUtil.ok(service.updatePolygon(dto));
    }


    /*
        SELECT
     */
    // 根据跟定点位 查询数据库是否有对应坐标
    @PostMapping("/queryPointByPoint")
    public ResultUtil<Radar> queryPointByPoint(@RequestBody Radar radar){
        return ResponseUtil.ok(service.queryPointByPoint(radar));
    }


    // 根据图形 查询数据库是否有在该图形中的坐标
    @PostMapping("/queryPointByGeometry")
    public ResultUtil<HashMap<String,List<Radar>>> queryPointByGeometry(@RequestBody List<Object> list){
        return ResponseUtil.ok(service.searchPoint(list));
    }


    //  查询所有的点
    @GetMapping("/queryAllPoint")
    public ResultUtil<List<Radar>> queryAllPoint(){

        return ResponseUtil.ok(service.queryAllPoint());
    }


    // 查询所有的线
    @GetMapping("/queryAllLineString")
    public ResultUtil<List<GeometryVo>> queryAllLineString(){
        return ResponseUtil.ok(service.queryAllLineString());
    }


    // 查询所有的面
    @GetMapping("/queryAllPolygon")
    public ResultUtil<List<GeometryVo>> queryAllPolygon(){
        return ResponseUtil.ok(service.queryAllPolygon());
    }

    @GetMapping("/test01")
    public ResultUtil<List<DmTrade>> queryDm() throws ParseException {
        DmTrade dm = new DmTrade();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        dm.setCreatedTime(sdf.parse("2015-04-13"));
        dm.setUpdatedTime(sdf.parse("2015-10-04"));
        ResultUtil<List<DmTrade>> listResultUtil = dmTradeService.queryDm(dm);
        return listResultUtil;
    }
}
